{{- if .Values.global.ingress.enabled -}}
{{- $fullName := include "affine.fullname" . -}}
{{- if and .Values.global.ingress.className (not (semverCompare ">=1.18-0" .Capabilities.KubeVersion.GitVersion)) }}
  {{- if not (hasKey .Values.global.ingress.annotations "kubernetes.io/ingress.class") }}
  {{- $_ := set .Values.global.ingress.annotations "kubernetes.io/ingress.class" .Values.ingress.className}}
  {{- end }}
{{- end }}
{{- if semverCompare ">=1.19-0" .Capabilities.KubeVersion.GitVersion -}}
apiVersion: networking.k8s.io/v1
{{- else if semverCompare ">=1.14-0" .Capabilities.KubeVersion.GitVersion -}}
apiVersion: networking.k8s.io/v1beta1
{{- else -}}
apiVersion: extensions/v1beta1
{{- end }}
kind: Ingress
metadata:
  name: {{ $fullName }}
  labels:
    {{- include "affine.labels" . | nindent 4 }}
  {{- with .Values.global.ingress.annotations }}
  annotations:
    {{- toYaml . | nindent 4 }}
  {{- end }}
spec:
  {{- if and .Values.global.ingress.className (semverCompare ">=1.18-0" .Capabilities.KubeVersion.GitVersion) }}
  ingressClassName: {{ .Values.global.ingress.className }}
  {{- end }}
  {{- if .Values.global.ingress.tls }}
  tls:
    {{- range .Values.global.ingress.tls }}
    - hosts:
        {{- range .hosts }}
        - {{ . | quote }}
        {{- end }}
      secretName: {{ .secretName }}
    {{- end }}
  {{- end }}
  rules:
    - host: "{{ .Values.global.ingress.host }}"
      http:
        paths:
          - path: /socket.io
            pathType: Prefix
            backend:
              service:
                name: affine-sync
                port:
                  number: {{ .Values.sync.service.port }}
          - path: /graphql
            pathType: Prefix
            backend:
              service:
                name: affine-graphql
                port:
                  number: {{ .Values.graphql.service.port }}
          - path: /api
            pathType: Prefix
            backend:
              service:
                name: affine-graphql
                port:
                  number: {{ .Values.graphql.service.port }}
          - path: /oauth
            pathType: Prefix
            backend:
              service:
                name: affine-graphql
                port:
                  number: {{ .Values.graphql.service.port }}
          - path: /
            pathType: Prefix
            backend:
              service:
                name: affine-web
                port:
                  number: {{ .Values.web.service.port }}
          - path: /js/worker.(.+).js
            pathType: ImplementationSpecific
            backend:
              service:
                name: affine-web
                port:
                  number: {{ .Values.web.service.port }}
{{- end }}
